package com.ywb.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywb.mapper.CategoryMapper;
import com.ywb.mapper.CommodityMapper;
import com.ywb.pojo.*;
import com.ywb.service.CategoryService;
import com.ywb.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) {
         categoryMapper.deleteById(id);
         commodityMapper.deleteByCategoryId(id);
    }

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public Category getById(Integer categoryid) {
        return categoryMapper.getById(categoryid);
    }

    @Override
    public void update(Category category) {
        categoryMapper.update(category);
    }

    @Override
    public PageResult<Category> page(CategoryQueryParam categoryQueryParam) {
        PageHelper.startPage(categoryQueryParam.getPage(), categoryQueryParam.getPageSize());
        List<Category> categories = categoryMapper.list(categoryQueryParam);
        Page<Category> p= (Page<Category>) categories;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
}
